﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using Phatent.Model.Results;
using Phatent.Model.Pages;
using Phatent.Logic.Pages;

namespace Phatent.Website.Areas.Admin.Controllers
{
    [RouteArea("Admin")]
    [RoutePrefix("Page")]
    [Authorize]
    public class PageApiController : ApiController
    {
        [Route("GetPages")]
        [HttpPost]
        public JsonEntitySet<PageModel> GetPages(JObject request)
        {
            string errinfo = "no data";
            try
            {
                PageQuery sea = new PageQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.CaseID = request.Value<int>("caseID");

                sea.Visible = request.Value<bool?>("visible");
                var result = PageLogic.Gets(sea);
                return new JsonEntitySet<PageModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                //Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<PageModel>(null, errinfo, false);
        }


        [Route("GetCases")]
        [HttpPost]
        public JsonEntitySet<CaseModel> GetCases(JObject request)
        {
            string errinfo = "no data";
            try
            {
                CaseQuery sea = new CaseQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.PID = request.Value<int>("caseID");
                sea.OnlySubCase = request.Value<bool>("onlySubCase");
                sea.GetParent = request.Value<bool>("getParent");
                var result = CaseLogic.Gets(sea);
                return new JsonEntitySet<CaseModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntitySet<CaseModel>(null, errinfo, false);
        }

        [Route("SavePage")]
        [HttpPost]
        public JsonEntity<PageModel> SavePage(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                PageModel model = new PageModel();
                model.ID = request.Value<int>("pageID");
                model.CaseID = request.Value<int>("caseID");
                model.Title = request.Value<string>("title");
                model.Thumbnail = request.Value<string>("thumbnail");
                model.Pindex = request.Value<int>("pindex");
                model.Visible = request.Value<bool>("visible");
                model.FriendUrl = request.Value<string>("friendUrl");
                model.Abbreviation = request.Value<string>("abbreviation");
                model.PageType = request.Value<int>("pageType");
                model.ETitle = request.Value<string>("etitle");
                model.EAbbreviation = request.Value<string>("eabbreviation");
                #endregion

                if (model.ID == 0)
                    model = PageLogic.CreateNewPage(model);
                else
                    PageLogic.UpdatePage(model);
                return new JsonEntity<PageModel>(model);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntity<PageModel>(null, errinfo, false);
        }

        [Route("SavePageContent")]
        [HttpPost]
        public JsonEntity<PageModel> SavePageContent(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                var id = request.Value<int>("pageID");
                var model = PageLogic.GetPage(id);
                if (model != null)
                {
                    model.Description = request.Value<string>("description");
                    model.EDescription = request.Value<string>("edescription");
                    if (Phatent.Website.WebContext.CurrentContext.User.Identity.Name == "admin")
                        model.Visible = true;
                    else
                        model.Visible = false;
                    PageLogic.UpdatePage(model);
                    if (Phatent.Website.WebContext.CurrentContext.User.Identity.Name != "admin")
                    {
                        org.hengfeng.www.SmsInterface si = new org.hengfeng.www.SmsInterface();
                        si.Send("10999999", "13806691625", "温馨提示：大知网站（http://www.phatent.cn）有新的内容需要审核，请关注。", DateTime.Now);
                    }
                    
                    return new JsonEntity<PageModel>(model);
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntity<PageModel>(null, errinfo, false);
        }

        [Route("DeletePage")]
        [HttpPost]
        public JsonResult<bool> DeletePage(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("pageID");
                #endregion
                PageLogic.DeletePage(id);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("SetVisible")]
        [HttpPost]
        public JsonResult<bool> SetVisible(JObject request)
        {
            string errinfo = "对不起！审核失败，出错原因：";
            try
            {
                var id = request.Value<int>("pageID");
                PageLogic.SetVisible(id);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
